layui.use(['element', 'layer', 'form', 'table','laydate'], function () {
    var element = layui.element;
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    var laydate = layui.laydate;
    var tableInfo = table.render({
        id: 'myOrderInfoTableId',
        elem: '#myOrderInfoTable',
        url: '/front/myCenter/myOrder/myPushOrderInfoList',
        method: 'post',
        page: true,
        limit: 10,
        loading: true,
        skin: 'row',
        even: true,
        text: {
            none: '暂无相关数据'
        },
        cols: [[
            {checkbox: true, fixed: true},
            {field: 'id', title: '租赁单号', fixed: 'left'}
            , {field: 'landlordId', title: '甲方'}
            , {field: 'tenantId', title: '乙方', align: 'center'}
            , {field: 'hourseId', title: '房子ID', align: 'center'}
            , {field: 'statusStr', title: '状态', align: 'center'}
            , {field: 'createTime', title: '生成时间', sort: true, unresize: true, align: 'center'}
            , {fixed: 'right', title: '操作', width: 200, align: 'center', unresize: true, toolbar: '#oreationBtn'}
        ]]
    });

    table.on('tool(myOrderInfoTable)', function (obj) {
        var layEvent = obj.event;
        if (layEvent == 'detail') { //详情
            detailFunction(obj);
        } else if (layEvent === 'del') { //删除
            deleteFunction(obj);
        }
    });

    function detailFunction(obj) {
        var content = $('#orderDetailView');
        var detailInfo = JSON.parse(obj.data.detailInfo);
        layer.open({
            type: 1,
            skin: 'layui-layer-lan',
            title: ['单号: '+obj.data.id+" =============  创建时间:  "+obj.data.createTime, 'font-size:18px;'],
            closeBtn: 1,
            shade: 0,
            content: content,
            area: ['100%', '100%'],
            shadeClose: true,
            resize: true,
            anim: 1,
            id: 'LAY_layuidetailsadas',
            moveType: 1,
            maxmin: false,
            success: function (layero, index) {
                if(obj.data.status == 0) {
                    $('#updateOrderDetailBtn').show();
                    $('#number').removeAttr("disabled");
                    $('#rent').removeAttr("disabled");
                    $('#waterRate').removeAttr("disabled");
                    $('#electricityFee').removeAttr("disabled");
                    $('#extralFee').removeAttr("disabled");
                    $('#payRentTime').removeAttr("disabled");
                    $('#moveTime').removeAttr("disabled");
                    $('input[name="longRent"]').removeAttr("disabled");
                    $('#lease').removeAttr("disabled");
                    $('#effectTime').removeAttr("disabled");
                    $('#memberIdCardStr').removeAttr("disabled");
                    $('#txt').removeAttr("disabled");
                } else {
                    $('#updateOrderDetailBtn').hide();
                    $('#number').attr("disabled",true);
                    $('#rent').attr("disabled",true);
                    $('#waterRate').attr("disabled",true);
                    $('#electricityFee').attr("disabled",true);
                    $('#extralFee').attr("disabled",true);
                    $('#payRentTime').attr("disabled",true);
                    $('#moveTime').attr("disabled",true);
                    $('input[name="longRent"]').attr("disabled",true);
                    $('#lease').attr("disabled",true);
                    $('#effectTime').attr("disabled",true);
                    $('#memberIdCardStr').attr("disabled",true);
                    $('#txt').attr("disabled",true);
                }
                $('#number').val(detailInfo.number);
                $('#rent').val(detailInfo.rent);
                $('#waterRate').val(detailInfo.metaData.waterRate);
                $('#electricityFee').val(detailInfo.metaData.electricityFee);
                $('#extralFee').val(detailInfo.metaData.extralFee);
                $('#payRentTime').val(detailInfo.payRentTime);
                $('#moveTime').val(detailInfo.moveTime);
                $('[name="longRent"][value="'+detailInfo.longRent+'"]').prop('checked', true);
                $('#lease').val(detailInfo.lease);
                $('#effectTime').val(detailInfo.effectTime);
                var memberDetail = '';
                var jsonObject = detailInfo.membersIdCard;
                for (var k in jsonObject) {
                    var value = k+','+jsonObject[k];
                    memberDetail += value;
                    memberDetail += ';'
                }
                $('#memberIdCardStr').val(memberDetail);
                $('#txt').val(detailInfo.txt);
                form.render();
                $('#houseInfo').attr("href",'/front/myCenter/houseInfo/frontHouseInfo?houseId='+obj.data.hourseId);
                form.on('submit(updateOrderDetailBtn)', function(data){
                    var params = $.param({
                        id:obj.data.id,
                        number: data.field.number,
                        rent: data.field.rent,
                        waterRate: data.field.waterRate,
                        electricityFee: data.field.electricityFee,
                        extralFee: data.field.extralFee,
                        payRentTime: data.field.payRentTime,
                        moveTime: data.field.moveTime,
                        longRent: data.field.longRent,
                        lease: data.field.lease,
                        effectTime: data.field.effectTime,
                        memberIdCardStr: data.field.memberIdCardStr,
                        txt: data.field.txt
                    }, true);
                    $.ajax({
                        url: '/front/myCenter/myOrder/updateDetail',
                        type: 'post',
                        data: params,
                        dataType: 'json',
                        success: function (e) {
                            layer.msg(e.msg);
                            if (e.msg == "订单详情更新成功") {
                                layer.close(index);
                                tableInfo.reload({ //执行重载
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    },
                                    done: function (res, curr, count) {
                                        this.where = {};
                                    }
                                });
                            }
                        }
                    });
                    return false;
                });
            },
            cancel: function (index, layero) {

            }
        });
    }

    function deleteFunction(obj) {
        layer.open({
            type: 1,
            title: false,
            closeBtn: 2,
            resize: true,
            content: '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">' +
            '(请慎重)是否需要删除订单?删除后将不可恢复</div>',
            area: '30%',
            shade: 0.8,
            shadeClose: true,
            anim: 1,
            id: 'LAY_layuiprodel',
            btn: ['绝不后悔', '我怂了'],
            moveType: 1,
            yes: function (index, layero) {
                var params = $.param({
                    id: obj.data.id
                }, true);
                $.ajax({
                    url: '/front/myCenter/myOrder/deleteById',
                    type: 'post',
                    data: params,
                    dataType: 'json',
                    success: function (e) {
                        layer.msg(e.msg);
                        layer.close(index);
                        if (e.msg == "删除成功") {
                            obj.del();
                        }
                    }
                })
            }
        });
    }

    var active = {
        deletebatchInfo:function () {
            var checkStatus = table.checkStatus('myOrderInfoTableId')
                , data = checkStatus.data;
            if (data.length > 50) {
                layer.msg("超过最大数量限制");
                return false;
            }
            layer.open({
                type: 1,
                title: false,
                closeBtn: 2,
                resize: true,
                content: '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">' +
                '是否需要删除?删除后将不可恢复</div>',
                area: '30%',
                shade: 0.8,
                shadeClose: true,
                anim: 1,
                id: 'LAY_layuiprodel', //设定一个id，防止重复弹出
                btn: ['绝不后悔', '我怂了'],
                moveType: 1,
                yes: function (index, layero) {
                    var params = $.param({
                        mydata: JSON.stringify(data)
                    }, true);
                    $.ajax({
                        url: '/front/myCenter/myOrder/deleteBatchById',
                        type: 'post',
                        data: params,
                        dataType: 'json',
                        success: function (e) {
                            layer.msg(e.msg);
                            layer.close(index);
                            if (e.msg == "删除成功") {
                                tableInfo.reload({ //执行重载
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    },
                                    done: function (res, curr, count) {
                                        this.where = {};
                                    }
                                });
                            }
                        }
                    })
                }
            });
        },
        findOrderInfo: function () {
            var content = $('#findByPropertyView');
            layer.open({
                type: 1,
                skin: 'layui-layer-molv',
                title: ['查询维度', 'font-size:18px;'],
                closeBtn: 1,
                shade: 0.3,
                content: content,
                area: ['70%', '70%'],
                shadeClose: false,
                resize: true,
                anim: 1,
                id: 'LAY_layuirofind', //设定一个id，防止重复弹出
                moveType: 1,
                maxmin: false,
                success: function (layero, index) {
                    $('#f_id').val('');
                    $('#f_tenantId').val('');
                    $('#createTimeScope').val('');
                    $('#f_status').val(0);
                    form.render();
                    var active = {
                        findOrderInfo: function () {
                            var id = $('#f_id').val();
                            var tenantId = $('#f_tenantId').val();
                            var createTimeScope = $('#createTimeScope').val();
                            var status = $('#f_status').val();
                            var params = {
                                id: id,
                                tenantId: tenantId,
                                createTimeScope: createTimeScope,
                                status: status
                            };
                            tableInfo.reload({
                                where: {
                                    key: JSON.stringify(params) //转成json
                                },
                                page: {
                                    curr: 1 //重新从第 1 页开始
                                },
                                done: function (res, curr, count) {
                                    this.where = {};
                                    layer.close(index);
                                }
                            });
                        }
                    };
                    $('.findOrderByProperty .layui-btn').on('click', function () {
                        var type = $(this).data('type');
                        active[type] ? active[type].call(this) : '';
                    });
                },
                cancel: function (index, layero) {
                    $('#f_id').val('');
                    $('#f_landlordId').val('');
                    $('#f_tenantId').val('');
                    $('#createTimeScope').val('');
                    $('#f_status').val(0);
                    form.render();
                }
            })
        }
    };

    $('.AllBtnController .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    laydate.render({
        elem: '#createTimeScope',
        type: 'datetime',
        range: true,
        format: 'yyyy-MM-dd HH:mm:ss',
        calendar: true
    });
});